#include "widget.h"
#include "ui_widget.h"
#include <QSound>


Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    ui->setupUi(this);
    setWindowTitle("xiao");
    setWindowFlags(Qt::Window|Qt::FramelessWindowHint);
    setAttribute(Qt::WA_TranslucentBackground);
    setWindowFlags(Qt::FramelessWindowHint |Qt::WindowStaysOnTopHint);
    QImage*renwu=new QImage;
            renwu->load(":/new/prefix2/renwu.png");
            ui->label->setPixmap(QPixmap::fromImage(*renwu));

}

void Widget::mouseMoveEvent(QMouseEvent *event)
{
    if (event->buttons()&Qt::LeftButton)
    {
        if (ifdragging)
        {
            QPoint delta = event->globalPos() - startPosition;
            move(framePosition + delta);

        }
    }
    QWidget::mouseMoveEvent(event);

}

void Widget::mousePressEvent(QMouseEvent *event)
{
    if (event->button() == Qt::LeftButton)
    {
        ifdragging = true;
        startPosition = event->globalPos();
        framePosition = frameGeometry().topLeft();
        ui->label->clear();
        QImage*niao=new QImage;
                niao->load(":/new/prefix2/tuozhuai.png");
                ui->label->setPixmap(QPixmap::fromImage(*niao));
    }
    QWidget::mousePressEvent(event);
    n--;
    ui->jishu->setText(QString::number(n));
}

void Widget::mouseReleaseEvent(QMouseEvent *event)
{
    ifdragging = false;
    ui->label->clear();
    QImage*rw=new QImage;
            rw->load(":/new/prefix2/renwu.png");
            ui->label->setPixmap(QPixmap::fromImage(*rw));
    QWidget::mouseReleaseEvent(event);

}

Widget::~Widget()
{
    delete ui;
}

void Widget::on_pushButton_clicked()
{

    if(sw==0||sw==9||sw==2||sw==1||sw==7||sw==8||sw==4||sw==3){
        QLabel* labelImage = new QLabel(this, Qt::Dialog |Qt::WindowCloseButtonHint);
                labelImage->setWindowTitle("心情");
                QString imagePath = ":/new/prefix2/shangsheng.jpg";
                    QImage xin;
                    xin.load(imagePath);
                    labelImage->resize(300,200);
                    labelImage->setPixmap(QPixmap::fromImage(xin.scaled(300, 200, Qt::KeepAspectRatio)));
                labelImage->show();}
    if(sw==5||sw==6){
            QLabel* labelImage = new QLabel(this, Qt::Dialog |Qt::WindowCloseButtonHint);
                    labelImage->setWindowTitle("心情");
                    QString imagePath = ":/new/prefix2/xiajiang.jpg";
                        QImage xin;
                        xin.load(imagePath);
                        labelImage->resize(300,200);
                        labelImage->setPixmap(QPixmap::fromImage(xin.scaled(300, 200, Qt::KeepAspectRatio)));
                    labelImage->show();}
        if(sw==0||sw==1){
            n=n+3;
        }
        else if(sw==2){
            n++;
        }
        else if(sw==3){
            n=n+2;
        }
        else if(sw==4){
            n=n+2;
        }
        else if(sw==5){
            n=n-2;
        }
        else if(sw==6){
            n=n-3;
        }
        else if(sw==7){
            n=n+3;
        }
        else if(sw==8){
            n=n+2;
        }
        else if(sw==9){
            n++;
        }

        ui->jishu->setText(QString::number(n));
}

void Widget::on_pushButton_2_clicked()
{
    int z;
    srand(time(NULL));
    z=rand() % 10 + 1;
    if(n>z){
         ui->label->clear();
         QMovie *Movie = new QMovie(":/new/prefix1/timg.gif");
         ui->label->setMovie(Movie);
         Movie->start();
    }
    else if(n>-50&&n<z){
        QLabel* labelImage = new QLabel(this, Qt::Dialog |Qt::WindowCloseButtonHint);
            labelImage->setWindowTitle("回答");
            QString imagePath = ":/new/prefix2/x2.png";
                QImage xin;
                xin.load(imagePath);
                labelImage->resize(300,100);
                labelImage->setPixmap(QPixmap::fromImage(xin.scaled(250, 100, Qt::KeepAspectRatio)));
            labelImage->show();
    }
    else if(n<-50){
        QLabel* labelImage = new QLabel(this, Qt::Dialog |Qt::WindowCloseButtonHint);
            labelImage->setWindowTitle("回答");
            QString imagePath = ":/new/prefix2/x1.png";
                QImage xin;
                xin.load(imagePath);
                labelImage->resize(300,100);
                labelImage->setPixmap(QPixmap::fromImage(xin.scaled(250, 100, Qt::KeepAspectRatio)));
            labelImage->show();
            this->hide();
    }
}

void Widget::recvkaichang()
{
    this->show();
}

void Widget::on_beibao_clicked()
{

    beibao *configWindow = new beibao;
        configWindow->show();

}

void Widget::on_pushButton_3_clicked()
{
    if(sw==1){
            ui->pushButton->setStyleSheet("QPushButton{background-image: url(:/new/prefix1/meimeng.jpg);}");
     }
    else if(sw==2){
            ui->pushButton->setStyleSheet("QPushButton{background-image: url(:/new/prefix1/1.jpg);}");
     }
    else if(sw==3){
            ui->pushButton->setStyleSheet("QPushButton{background-image: url(:/new/prefix1/2.jpg);}");
     }
    else if(sw==4){
            ui->pushButton->setStyleSheet("QPushButton{background-image: url(:/new/prefix1/3.jpg);}");
     }
    else if(sw==5){
            ui->pushButton->setStyleSheet("QPushButton{background-image: url(:/new/prefix1/4.jpg);}");
     }
    else if(sw==6){
            ui->pushButton->setStyleSheet("QPushButton{background-image: url(:/new/prefix1/5.jpg);}");
     }
    else if(sw==7){
            ui->pushButton->setStyleSheet("QPushButton{background-image: url(:/new/prefix1/6.jpg);}");
     }
    else if(sw==8){
            ui->pushButton->setStyleSheet("QPushButton{background-image: url(:/new/prefix1/7.jpg);}");
     }
    else if(sw==9){
            ui->pushButton->setStyleSheet("QPushButton{background-image: url(:/new/prefix1/pinguo.jpg);}");
     }

}

void Widget::on_checkBox_stateChanged(int arg1)
{
     QSound *startsound=new QSound(":/new/prefix3/BGM.WAV",this);
    if(arg1==2){

        startsound->play();
        startsound->setLoops(-1);
    }
    else if(arg1==0){
        startsound->stop();
    }


}

void Widget::on_pushButton_4_clicked()
{
    QApplication* app;
    app->quit();
}
